#include "stsend.h"

STsend::STsend(QObject *parent): QThread(parent)
{

    _stop=false;
}

STsend::~STsend()
{

}

void STsend::run(){
    while(1){

        qm.lock();
        if(_stop){
            qm.unlock();
            break;
        }

        QVector<ClientList> activelist=getActiveClient(clientlist);
        int cont=activelist.length();
        if(cont>0){

          for(int i=0;i<_data.size();i++){
              int rd=rand()%cont;
               InMapData inmap;
               inmap.key=i;
               inmap.value=_data[i].toLatin1().data();
               SendWriteSignal(inmap,activelist[rd].socket);
              // connect(this, SIGNAL(Write(QByteArray,QTcpSocket *)),_p, SLOT(Write(QByteArray Data,QTcpSocket *sk)));
              //activelist[rd].socket->write("ddd");
              //activelist[rd].socket->waitForBytesWritten(1000);
          }
          _data.clear();
        }
        Sleep(2000);
        qm.unlock();
    }
}
void STsend::stop(){
    _stop=true;
}
void STsend::init(QVector<ClientList> *_clientlist, QMap<long, QString> data){
    clientlist=_clientlist;
    _data=data;


}

void STsend::SendWriteSignal(InMapData inmap,QTcpSocket* sk){
    emit WriteSignals(inmap,sk);
}



